<?php


namespace app\admin\controller\fzyj;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Ycorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $shop_id = $this->request->param('shop_id/d', 0);

        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['o.shop_id', '=', $shop_id];
            $where[] = ['o.is_yc', '=', 1];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $where[] = ['o.phone', 'like', '%' . $filter['phone'] . '%'];
                }

                if (isset($filter['yc'])) {
                    $where[] = ['o.yc', '=',$filter['yc']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['yc_time'])) {
                    $create_time = explode(' - ', $filter['yc_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.yc_time', 'between', [$create_time[0], $create_time[1]]];
                }

            }
            //获取列表
            $_list = Db::name('order')->alias('o')
                ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
                ->leftJoin('shop s', 'o.shop_id=s.id')
                ->field('o.*,sa.name as sa_name,sa.id as sa_id,s.id as s_id,s.shop_name')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['kf_status_time']) {
                    $_list[$k]['kf_status_time'] = date('Y-m-d H:i:s', $v['kf_status_time']);
                } else {
                    $_list[$k]['kf_status_time'] = '--------';
                }
                if ($v['cw_status_time']) {
                    $_list[$k]['cw_status_time'] = date('Y-m-d H:i:s', $v['cw_status_time']);
                } else {
                    $_list[$k]['cw_status_time'] = '--------';
                }
                if ($v['yc_time']) {
                    $_list[$k]['yc_time'] = date('Y-m-d H:i:s', $v['yc_time']);
                } else {
                    $_list[$k]['yc_time'] = '--------';
                }
            }
            //获取数量
            $total = Db::name('order')->alias('o')
                ->leftJoin('salesperson sa', 'o.salesperson_id=sa.id')
                ->leftJoin('shop s', 'o.shop_id=s.id')
                ->where($where)->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $this->assign([
            'proviec'=> $proviec,
            'shop_id'=>$shop_id
        ]);
        return $this->fetch('fzyj/ycorder/index');
    }

    public function ycedit()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update_data = array(
                'id' => $data['id'],
                'yc' => $data['row']['yc'],
                'money1' => $data['row']['money1'],
                'money2' => $data['row']['money2'],
                'is_z' => $data['row']['is_z'],
                'yc_desc' => $data['row']['yc_desc'],
                'is_yc'=>1,
                'yc_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['row']['money1']){
                //商家扣款
                $shop_info=Db::name('shop')->find($data['shop_id']);
                $member_info=Db::name('member')->find($shop_info['member_id']);
                setMoney($member_info['id'],0,$shop_info['id'],$data['row']['money1'],2,$data['id'],0,0,6);
            }
            if ($data['row']['money2']){
                $salesperson_info=Db::name('salesperson')->find($data['salesperson_id']);
                $member_info=Db::name('member')->find($salesperson_info['member_id']);
                //业务员扣款
                setMoney($member_info['id'],$salesperson_info['id'],0,$data['row']['money2'],2,$data['id'],0,0,5);
            }
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        $yc_list = array(
//            ['id' => 1,
//                'name' => '离网'
//            ],
            ['id' => 1,
                'name' => '管理单停'
            ],
            ['id' => 2,
                'name' => '管理停机'
            ],
            ['id' => 3,
                'name' => '欠费单停'
            ],
            ['id' => 4,
                'name' => '欠费停机'
            ],
            ['id' => 5,
                'name' => '销户'
            ],
            ['id' => 6,
                'name' => '涉诈'
            ],
            ['id' => 7,
                'name' => '其他'
            ]
        );
        $this->assign([
            'data' => $order_info,
            'yc_list' => $yc_list,
        ]);
        return $this->fetch();
    }

    public function ycedit2()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $update_data = array(
                'id' => $data['id'],
                'yc' => $data['row']['yc'],
                'money1' => $data['row']['money1'],
                'money2' => $data['row']['money2'],
                'is_z' => $data['row']['is_z'],
                'yc_desc' => $data['row']['yc_desc'],
                'is_yc'=>1,
                'yc_time' => time(),
            );
            Db::name('order')->update($update_data);
            if ($data['row']['money1']){
                //商家扣款
                $shop_info=Db::name('shop')->find($data['shop_id']);
                $member_info=Db::name('member')->find($shop_info['member_id']);
                setMoney($member_info['id'],0,$shop_info['id'],$data['row']['money1'],2,$data['id'],0,0,6);
            }
            if ($data['row']['money2']){
                $salesperson_info=Db::name('salesperson')->find($data['salesperson_id']);
                $member_info=Db::name('member')->find($salesperson_info['member_id']);
                //业务员扣款
                setMoney($member_info['id'],$salesperson_info['id'],0,$data['row']['money2'],2,$data['id'],0,0,5);
            }
            $this->success('操作成功！');
        }
        $order_info = Db::name('order')->find($id);
        $yc_list = array(
            ['id' => 1,
                'name' => '管理单停'
            ],
            ['id' => 2,
                'name' => '管理停机'
            ],
            ['id' => 3,
                'name' => '欠费单停'
            ],
            ['id' => 4,
                'name' => '欠费停机'
            ],
            ['id' => 5,
                'name' => '销户'
            ],
            ['id' => 6,
                'name' => '涉诈'
            ],
            ['id' => 7,
                'name' => '其他'
            ]
        );
        $this->assign([
            'data' => $order_info,
            'yc_list' => $yc_list,
        ]);
        return $this->fetch();
    }

}